package com.os.common.validator.impl;

import com.os.common.validator.CheckSpecialCharacters;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;
@Slf4j
//Spring如何实现一个校验特殊字符的自定义注解  20240610
//https://www.cnblogs.com/Chary/p/18006593
public class CheckSpecialCharactersValidator implements ConstraintValidator<CheckSpecialCharacters, String> {
    private Pattern specialCharactersPattern = Pattern.compile("\\p{Punct}");

    @Override
    public void initialize(CheckSpecialCharacters constraintAnnotation) {
        // 初始化代码，如果需要的话
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        log.info("******************isValid.value=********************************"+ value);
        if (value == null || value.isEmpty()) {
            return true;
        }
        //return false;
        return !specialCharactersPattern.matcher(value).find();
    }
}